package com.com.knowledgepaymentmanagementsystem.controller.user;

import com.com.knowledgepaymentmanagementsystem.anno.Log;
import com.com.knowledgepaymentmanagementsystem.pojo.user.User;
import com.com.knowledgepaymentmanagementsystem.service.user.UserService;
import com.com.knowledgepaymentmanagementsystem.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 允许跨域调度
@CrossOrigin

@Api(tags = "用户管理")
// 声明控制层
@RestController
// 类映射
@RequestMapping("user")
public class UserController {
    // 注入 UserService 接口
    @Autowired
    private UserService userService;

    // 查询用户所有数据
    @ApiOperation(value = "查询用户", httpMethod = "POST", notes = "查询用户所有条目")
    @PostMapping("findUserList")
    public ResultUtil<List<User>> findUserList(@RequestBody User user) {

        try {
            return new ResultUtil<>(200, "查询用户数据成功", userService.findUserList(user));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "查询用户数据失败");
        }
    }

    // 删除用户表中数据（根据 ID 删除）
    @Log
    @ApiOperation(value = "删除用户", httpMethod = "DELETE", notes = "根据ID删除用户")
    @DeleteMapping("delUserById")
    public ResultUtil delUserById(@RequestParam Integer id) {
        try {

            // 处理请求
            userService.delUserById(id);
            return new ResultUtil<>(200, "删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "删除用户失败");
        }

    }

    // 添加用户
    @Log
    @ApiOperation(value = "添加用户", httpMethod = "POST", notes = "添加用户")
    @PostMapping("addUser")
    public ResultUtil<String> addUser(@RequestBody User user) {

        try {
            // 处理添加请求
            return new ResultUtil<>(200, "添加用户成功",userService.addUser(user));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "添加用户失败");
        }
    }

    // 修改用户数据
    @Log
    @ApiOperation(value = "修改用户", httpMethod = "PUT", notes = "修改用户")
    @PutMapping("updataUser")
    public ResultUtil updataUser(@RequestBody User user) {

        try {
            // 处理修改请求
            userService.updataUser(user);
            return new ResultUtil<>(200, "修改用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtil<>(500, "修改用户失败");
        }
    }
}
